package o2sjsc.activity;

import java.util.ArrayList;
import java.util.HashMap;

import o2sjsc.core.libs.loadImage.ImageLoader;
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class LazyAdapter extends BaseAdapter {

	Activity activity;
	ArrayList<HashMap<String, String>> data;
	LayoutInflater inflater;
	private ImageLoader imageLoader;

	public LazyAdapter(Activity a, ArrayList<HashMap<String, String>> d) {
		this.activity = a;
		this.data = d;
		inflater = (LayoutInflater) a
				.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
		imageLoader = new ImageLoader(activity.getApplicationContext());
	}

	@Override
	public int getCount() {
		if (data != null) {
			return data.size();
		} else {
			return 0;
		}
	}

	@Override
	public Object getItem(int position) {
		return position;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		if (convertView == null)
			convertView = inflater.inflate(R.layout.list_row, null);

		TextView contentId = (TextView) convertView
				.findViewById(R.id.contentId);
		TextView title = (TextView) convertView.findViewById(R.id.title);
		TextView description = (TextView) convertView
				.findViewById(R.id.desciption);
		ImageView thumbImage = (ImageView) convertView
				.findViewById(R.id.thumbImage);

		HashMap<String, String> item = new HashMap<String, String>();
		item = data.get(position);

		contentId.setText(item.get("CONTENT_ID"));
		title.setText(item.get("FULL_TITLE"));
		description.setText(item.get("FULL_DESCRIPTION"));

		imageLoader.DisplayImage(item.get(NewsAsyncTask.THUMB_IMG), thumbImage);
		return convertView;
	}

}
